package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.TbPartnerMapper;
import com.heima.pojo.TbPartner;
import com.heima.pojo.dto.PartnerDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.TbPartnerVo;
import com.heima.service.ITbPartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 合作商表 服务实现类
 * </p>
 *
 * @author nan
 * @since 2024-06-07
 */
@Service
public class TbPartnerServiceImpl extends ServiceImpl<TbPartnerMapper, TbPartner> implements ITbPartnerService {

    @Autowired
    private TbPartnerMapper partnerMapper;
    /**
     * 合作商搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageResponse<TbPartnerVo> getByPage(Integer pageIndex, Integer pageSize, String name) {

        Page<TbPartner> page = new Page<>(pageIndex,pageSize);

        Page<TbPartnerVo> tbPartnerPage =partnerMapper.getByPage(page, name);
        PageResponse<TbPartnerVo> pageResponse = new PageResponse<>();
        pageResponse.setPageIndex(tbPartnerPage.getCurrent());
        pageResponse.setPageSize(tbPartnerPage.getSize());
        pageResponse.setTotalPage(tbPartnerPage.getPages());
        pageResponse.setTotalCount(tbPartnerPage.getTotal());
        pageResponse.setCurrentPageRecords(List.copyOf(tbPartnerPage.getRecords()));
        return pageResponse;
    }

    /**
     * 修改合作商
     *
     * @param id
     * @param partnerDto
     * @return
     */
    @Override
    public Boolean updatePartner(Integer id, PartnerDto partnerDto) {
        TbPartner tbPartner = BeanUtil.toBean(partnerDto, TbPartner.class);
        tbPartner.setId(id);
        return updateById(tbPartner);
    }
}
